'use strict';

module.exports = app => {
  return class extends app.Controller {
    constructor(ctx) {
      super(ctx);

      this.createRule = {
        content: { type: 'string', required: true },
        comment_id: { type: 'string', format: /\d+/, required: true },
        reply_id: { type: 'number', format: /\d+/, required: false },
        from_uid: { type: 'number', format: /\d+/, required: true },
        to_uid: { type: 'number', format: /\d+/, required: true },
      };
    }

    async index() {
      const { ctx } = this;
      const query = Object.assign(ctx.query, ctx.params)

      ctx.validate({
        comment_id: { type: 'string', format: /\d+/, required: true },
        page: { type: 'number', format: /\d+/, required: false },
        limit: { type: 'number', format: /\d+/, required: false },
      }, query)

      ctx.body = await ctx.service.reply.list({
        comment_id: query.comment_id,
        page: query.page || 1,
        limit: query.limit || 10,
      });
    }

    async create() {
      const { ctx } = this;
      const query = Object.assign(ctx.request.body, ctx.params)
      ctx.validate(this.createRule, query);
      const id = await ctx.service.reply.create(query);
      ctx.body = {
        id,
      };
      ctx.status = 201;
    }

  }
}
